package com.example.com.ddai.test;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.autonavi.minimap.net.DHttpClinet;
import com.autonavi.minimap.util.cache.CacheWorker;
import com.autonavi.minimap.util.cache.ThumbnailLoader;
import com.autonavi.minimap.widget.WebImageView;
import com.example.com.ddai.others.Source;

public class MainActivity extends Activity {
	GridView list;
	MyAdapter mAdapter;
	Images data = new Images();
	ThumbnailLoader loader;

	private void initData() {
		new Thread() {
			public void run() {
				final ArrayList<Source> sources = data.loadData();
				if (BuildConfig.DEBUG) {
					Log.d("DDAI", "GET DATA = " + sources.size());
				}
				runOnUiThread(new Runnable() {

					@Override
					public void run() {
						mAdapter.addSource(sources);
						mAdapter.notifyDataSetChanged();
					}
				});
			};
		}.start();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		loader = ThumbnailLoader.getInstance(getApplicationContext(), "",false);
		list = (GridView) findViewById(R.id.gridView1);
		mAdapter = new MyAdapter(this);
		list.setAdapter(mAdapter);
		initData();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getTitle().equals("TEST TASK")) {
			Intent intent = new Intent(this, TestActivity.class);
			startActivity(intent);
		}
		if ("TEST DATA".equals(item.getTitle())) {
			data.testResources();
		}
		if ("Clean Cache".equals(item.getTitle())) {
			new Thread() {
				public void run() {
					loader.reserLoader();
					int r = loader.cleanDiskCache();
					Log.d("DDAI", "Delete =" + r);
				};
			}.start();
		}
		if("TEST1".equals(item.getTitle())){
			new Thread() {
				public void run() {
					File path = new File(Environment
							.getExternalStorageDirectory(), "ddai");
					File imageFile = new File(path,"temp.jpg");
					Log.d("DDAI", "imageFile="+imageFile.getAbsolutePath());
					DHttpClinet client = new DHttpClinet();
					try {
						client.downloadInFile("http://ddaitp.sinaapp.com/goods/cover Deliver Us From Evil.jpg", imageFile, getApplicationContext());
					} catch (IOException e) {
						e.printStackTrace();
					}
				};
			}.start();
		}
		return true;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		menu.add("TEST TASK");
		menu.add("TEST DATA");
		menu.add("Clean Cache");
		menu.add("TEST1");
		return true;
	}

	private final class MyAdapter extends BaseAdapter {
		private LayoutInflater mInflater;

		public MyAdapter(Activity activity) {
			mInflater = LayoutInflater.from(activity);
		}

		ArrayList<Source> sources = new ArrayList<Source>();

		public void addSource(ArrayList<Source> news) {
			sources.addAll(news);
		}

		@Override
		public int getCount() {
			return sources.size();
		}

		@Override
		public Object getItem(int position) {
			return sources.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;

			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_item, null);
				holder = new ViewHolder();
				holder.icon = (WebImageView) convertView
						.findViewById(R.id.imageView1);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			// Bind the data efficiently with the holder.
			Source s = sources.get(position);
			CacheWorker.Builder cp = new CacheWorker.Builder(0,0);
			cp.setGreyImage(true);
			loader.loadRemoteImage(s.pic, holder.icon.getImageView(), cp, 
					holder.icon.getLoadingListener());
			return convertView;
		}

		private class ViewHolder {
			WebImageView icon;
		}
	}
}
